<?php
namespace frontend\models;

use yii;
use yii\base\Model;
use common\models\User;
use common\helper\MobileEmail;
use yii\validators\EmailValidator;
/**
 * Signup form
 */
class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $flag;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'required', 'message'=>'手机或邮箱必填'],
            ['username', 'match', 'pattern'=>'/^1[0-9]{10}$/','message'=>'{attribute}必须为1开头的11位纯数字', 'on'=>'mobile'],
            ['username', 'email', 'message'=>'请填写正确格式的邮箱地址', 'on'=>'email'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '已经存在'],

//            ['email', 'filter', 'filter' => 'trim'],
//            ['email', 'required'],
//            ['email', 'email'],
//            ['email', 'string', 'max' => 255],
//            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

            ['password', 'required', 'message'=>'密码必填'],
            ['password', 'string', 'min' => 6],
        ];
    }



    public function isMobile($mobile) {
        if (!is_numeric($mobile)) {
            return false;
        }
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }


    public function isEmail($email){
        $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
        if ( !preg_match( $pattern, $email ) ){
            return false;
        }
        return true;
    }


    //preg_match("/^1[34578]{1}\d{9}$/",$username)
    /**
     *  自定义验证B
     */
    public function requiredByASpecial($attribute, $params)
    {
        if ($this->mobile== 1)
        {
            if ($this->password=== "")
                $this->addError($attribute, "B的值不可以为空.");
        }
    }





    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if ($this->load(Yii::$app->request->post()) && $this->validate()) {
            $user = new User();
            switch ($this->flag){
                case 1:
                    $user->mobile = $this->username;
                    break;
                case 2:
                    $user->email = $this->username;
                    break;
            }
            $user->setPassword($this->password);
            $user->generateAuthKey();

            if ($user->save()) {
                if (Yii::$app->getUser()->login($user)) {
                    return Yii::$app->controller->goHome();
                }
            }else{
                return null;
            }
        }
    }



    public function attributeLabels()
    {
        return [
            'username'=>'用户名',
            'email'=>'邮箱',
        ];
    }


}
